package com.example.demo.TestController;

import com.example.demo.User.User;
import com.sun.deploy.net.cookie.CookieUnavailableException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Description:
 * User: 故我
 * Date: 2023-10-30
 * Time: 12:19
 */
@Slf4j
@RequestMapping("/test")
@RestController   //这个注解 = Controller（随着类加载而加载） + ResponseBody（返回的是一个数据非页面）
public class TestController {
    // 想要只支持post方法请求， method 参数为 post
    //1、1@RequestMapping(value = "/say", method = RequestMethod.POST)
    //@PostMapping("/say")
    @GetMapping("/say")
    public String Say(String name, int v) {
        return "hi,spring mvc! " + name + "| v: " + v; //支持get 也支持 post
    }

    // 接收普通对象
    @GetMapping("/user")
    public String getUser(User user) {
        return user.toString();
    }

    @GetMapping("/time")
    public String getTime(@RequestParam(value = "t", required = false) String startTime, @RequestParam("t2") String endTime) {
        return "开始时间：" + startTime + "| 结束时间：" + endTime;
    }

    // 接受json对象  从请求body中获取数据
    @PostMapping("/json-user")
    public String getUser2(@RequestBody User user) {
        return user.toString();
    }

    // 获取BaseURL中的参数
    @RequestMapping("/login/{username}/{passwd}")
    public String Login(@PathVariable("username") String username, @PathVariable("passwd") String passwd) {
        return username + " : " + passwd;
    }

    //这里的匹配不能出现一点不同
    @RequestMapping("/show/{username}/and/{passwd}")
    public String Show(@PathVariable("username") String username, @PathVariable("passwd") String passwd) {
        return username + " : " + passwd;
    }

    @RequestMapping("/upfile")
    public String upFile(@RequestPart("myfile") MultipartFile file) throws IOException {
        String path = "E:\\imag\\imag.png";
        //保存图片在哪里
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/myupfile")
    public String myupFile(@RequestPart("myfile") MultipartFile file) throws IOException {
        // 根目录
        String path = "E:\\imag\\";
        // 文件唯一标识
        path += UUID.randomUUID().toString().replace("-", "");//将所有的 - 替换成无
        // 文件的后缀名
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//从最后一个 . 开始进行分割
        file.transferTo(new File(path));
        return path;
    }

    // Spring MVC 内置了 servlet时代的对象
    @RequestMapping("/username")
    public String getUsername(HttpServletRequest request) {
        String name = request.getParameter("username");
        return name;
    }

    //获取cookie
    @RequestMapping("/cookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            log.error(cookie.getName() + " : " + cookie.getValue());
        }
        return "hello cookies!";
    }

    //获取单个的cookie
    @RequestMapping("/getck")
    public String getCk(@CookieValue("lisi") String value) {
        return "Cookie value: " + value;
    }

    //获取header
    @RequestMapping("/getHeader")
    public String getheader(@RequestHeader("User-Agent") String user_agent) {
        return user_agent;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //先设置session
        HttpSession session = request.getSession();//默认为 true 没有找到就创建
        session.setAttribute("userinfo", "tangchenhhh");
        return "Set Session Success!";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        // 看session对象中是否存储了session信息
        HttpSession session = request.getSession(false);//不在，就不会自动创建对象
        if (session != null && session.getAttribute("userinfo") != null) {
            //此时这个session信息存在
            return (String) session.getAttribute("userinfo");
        }else{
            return "暂无这个session信息!";
        }
    }

    @RequestMapping("/getSess")
    public String getSess(@SessionAttribute(value = "userinfo",required = false) String userinfo){
        return userinfo;
    }

    @RequestMapping("/forward")
    public String index1(){
        return "forward:/hello.html";
    }
}
